<?php
/**
 *title
 * @author: lican<364548233@qq.com>
 *Date: 2020/6/10
 *Time: 19:52
 */

namespace App\Http\Helpers;

use App\Http\Services\ApiService;
use App\Http\Services\SchoolService;
use App\Http\Services\MsgService;
use GeoIp2\Database\Reader;

class Sign{

    /**
     *匹配学号
     * @author: lican<364548233@qq.com>
     */
    public static function pregCardNumber($data){
        $cardNumber = $data['eventContent']['userId'];
        preg_match_all("/\((.*)\)/s",$cardNumber, $user, PREG_SET_ORDER );
        $cardNumber = $user[0][1];
        return $cardNumber;
    }

    /**
     *获取手机型号
     * @author: lican<364548233@qq.com>
     */
    public static function getPhoneModel(){
        $user_agent  =  $_SERVER [ 'HTTP_USER_AGENT' ];
        if  ( stripos ( $user_agent ,  "iPhone" )!==false) {
            $brand  =  'iPhone' ;
        }  else  if  ( stripos ( $user_agent ,  "SAMSUNG" )!==false ||  stripos ( $user_agent ,  "Galaxy" )!==false ||  strpos ( $user_agent ,  "GT-" )!==false ||  strpos ( $user_agent ,  "SCH-" )!==false ||  strpos ( $user_agent ,  "SM-" )!==false) {
            $brand  =  '三星' ;
        }  else  if  ( stripos ( $user_agent ,  "Huawei" )!==false ||  stripos ( $user_agent ,  "Honor" )!==false ||  stripos ( $user_agent ,  "H60-" )!==false ||  stripos ( $user_agent ,  "H30-" )!==false) {
            $brand  =  '华为' ;
        }  else  if  ( stripos ( $user_agent ,  "Lenovo" )!==false) {
            $brand  =  '联想' ;
        }  else  if  ( strpos ( $user_agent ,  "MI-ONE" )!==false ||  strpos ( $user_agent ,  "MI 1S" )!==false ||  strpos ( $user_agent ,  "MI 2" )!==false ||  strpos ( $user_agent ,  "MI 3" )!==false ||  strpos ( $user_agent ,  "MI 4" )!==false ||  strpos ( $user_agent ,  "MI-4" )!==false) {
            $brand  =  '小米' ;
        }  else  if  ( strpos ( $user_agent ,  "HM NOTE" )!==false ||  strpos ( $user_agent ,  "HM201" )!==false) {
            $brand  =  '红米' ;
        }  else  if  ( stripos ( $user_agent ,  "Coolpad" )!==false ||  strpos ( $user_agent ,  "8190Q" )!==false ||  strpos ( $user_agent ,  "5910" )!==false) {
            $brand  =  '酷派' ;
        }  else  if  ( stripos ( $user_agent ,  "ZTE" )!==false ||  stripos ( $user_agent ,  "X9180" )!==false ||  stripos ( $user_agent ,  "N9180" )!==false ||  stripos ( $user_agent ,  "U9180" )!==false) {
            $brand  =  '中兴' ;
        }  else  if  ( stripos ( $user_agent ,  "OPPO" )!==false ||  strpos ( $user_agent ,  "X9007" )!==false ||  strpos ( $user_agent ,  "X907" )!==false ||  strpos ( $user_agent ,  "X909" )!==false ||  strpos ( $user_agent ,  "R831S" )!==false ||  strpos ( $user_agent ,  "R827T" )!==false ||  strpos ( $user_agent ,  "R821T" )!==false ||  strpos ( $user_agent ,  "R811" )!==false ||  strpos ( $user_agent ,  "R2017" )!==false) {
            $brand  =  'OPPO' ;
        }  else  if  ( strpos ( $user_agent ,  "HTC" )!==false ||  stripos ( $user_agent ,  "Desire" )!==false) {
            $brand  =  'HTC' ;
        }  else  if  ( stripos ( $user_agent ,  "vivo" )!==false) {
            $brand  =  'vivo' ;
        }  else  if  ( stripos ( $user_agent ,  "K-Touch" )!==false) {
            $brand  =  '天语' ;
        }  else  if  ( stripos ( $user_agent ,  "Nubia" )!==false ||  stripos ( $user_agent ,  "NX50" )!==false ||  stripos ( $user_agent ,  "NX40" )!==false) {
            $brand  =  '努比亚' ;
        }  else  if  ( strpos ( $user_agent ,  "M045" )!==false ||  strpos ( $user_agent ,  "M032" )!==false ||  strpos ( $user_agent ,  "M355" )!==false) {
            $brand  =  '魅族' ;
        }  else  if  ( stripos ( $user_agent ,  "DOOV" )!==false) {
            $brand  =  '朵唯' ;
        }  else  if  ( stripos ( $user_agent ,  "GFIVE" )!==false) {
            $brand  =  '基伍' ;
        }  else  if  ( stripos ( $user_agent ,  "Gionee" )!==false ||  strpos ( $user_agent ,  "GN" )!==false) {
            $brand  =  '金立' ;
        }  else  if  ( stripos ( $user_agent ,  "HS-U" )!==false ||  stripos ( $user_agent ,  "HS-E" )!==false) {
            $brand  =  '海信' ;
        }  else  if  ( stripos ( $user_agent ,  "Nokia" )!==false) {
            $brand  =  '诺基亚' ;
        }  else  {
            $brand  =  '其他手机' ;
        }
        return $brand;
    }

    /**
     *日志
     * @author: lican<364548233@qq.com>
     */
    public static function writeLog($action, $data, $path = 'base')
    {
        $data = is_array($data) ? json_encode($data, JSON_FORCE_OBJECT) : $data;
        //设置目录时间
        $day = date('Ymd');

        //设置路径目录信息
        $url = './logs/texlog/' .$path.'/'. $day . '/' . $day . '_request_log.txt';

        //取出目录路径中目录(不包括后面的文件)
        $dirName = dirname($url);

        //如果目录不存在就创建
        if (!file_exists($dirName)) {
            //iconv防止中文乱码
            $res = mkdir(iconv("UTF-8", "GBK", $dirName), 0777, true);
        }
        $fp = fopen($url, "a");//打开文件资源通道 不存在则自动创建
        fwrite($fp, var_export(date('Y-m-d H:i:s') . '_' . $action . '_:' . $data, true) . "\r\n");//写入文件
        fclose($fp);//关闭资源通道
    }

    /**
     *通用签名计算
     * @author: lican<364548233@qq.com>
     */
    public static function colsign($data, $secret)
    {
        ksort($data);
        $param = array();
        foreach ($data as $k => $v) {
            if ($k != 'signature' && $k != 'sign') {
                if (is_array($v)) {
                    $v = json_encode($v);
                }
                $param[] = "$k=$v";
            }
        }
        $str = implode("&", $param);
        $str .= "&key=$secret";
        $sign = strtoupper(md5($str));

        self::writeLog(__CLASS__ . '/' . __FUNCTION__ . '/' . __LINE__, 'sign_str_:'.$str . '---sign_:' . $sign, 'signature');

        return $sign;
    }

    public static function getNonceStr($length = 32){
        return substr(strtoupper(md5(time() . mt_rand(0, 10000) . substr('weixiao', mt_rand(0, 7)))), 0, $length);
    }

    /**
     *递归分类
     * @author: lican<364548233@qq.com>
     */
    public static function generateTree($items){
        $tree = array();
        foreach($items as $item){
            if(isset($items[$item['parent_id']]) && $items[$item['parent_id']] != 0){
                $items[$item['parent_id']]['children'][] = &$items[$item['id']];
            }else{
                $tree[] = &$items[$item['id']];
            }
        }
        return $tree;
    }

    /**
     *获取用户登录ip
     * @author lican<364548233@qq.com>
     */
    public static function getClientIp() {
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] AS $xip) {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }




    /**
     *ip获取城市
     * @author lican<364548233@qq.com>
     */
    public static function getCity($ip = '')
    {
        $ip = trim($ip);
        $url = "https://restapi.amap.com/v3/ip?ip={$ip}&output=json&key=" . MAP;
        $res = file_get_contents($url);
        $city = json_decode($res, true)['city'];
        return $city;
    }

}
